
#include "ximage.h"

#include "xbitmap.h"

#include <QtGui>
#include <QPainter>
#include <QPen>
#include <QFile>

#include <QMessageBox>

XImage::XImage(QWidget *parent) : QWidget(parent)
{
    m_mt = 0;
    m_nt = 0;
    m_buf_size = 0;
    m_qimg = NULL;

    m_m = m_n = 0;
    m_x = m_y = 0;

    m_translated = false;
}

void XImage::set_pos()
{
    if (m_mt > m_nt) {
        if(m_mt > this->height()) {
            m_m = this->height();
            m_n = (m_nt*(double)m_m/(double)m_mt);
        } else {
            m_m = m_mt;
            m_n = m_nt;
        }
    }else{
        if(m_nt > this->width()) {
            m_n = this->width();
            m_m = (m_mt*(double)m_n/(double)m_nt);
        } else {
            m_m = m_mt;
            m_n = m_nt;
        }
    }

    m_x = (this->width() - m_n ) / 2;
    m_y = (this->height() - m_m ) / 2;
}

void XImage::resizeEvent(QResizeEvent *event)
{

    set_pos();

    QWidget::resizeEvent(event);
}

void XImage::paintEvent(QPaintEvent *)
{
    if(!m_qimg) return;


    QPainter painter(this);

    if(m_qimg != NULL)
        painter.drawPixmap(m_x,m_y,m_n,m_m,QPixmap::fromImage(*m_qimg));
}


int XImage::load_bitmap(XBitmap *bmp)
{
    // 如果文件名为空
    if(bmp == NULL){
        return -1;
    }

    uchar *buf = new uchar[bmp->get_buf_size()];
    if (buf == NULL) return -2;

    m_mt = bmp->get_mt();
    m_nt = bmp->get_nt();
    m_buf_size = bmp->get_buf_size();

    set_pos();

    char *bmp_buf = bmp->get_buf();
    for(unsigned long m = 0; m < m_mt; m++) {
        for(unsigned long n = 0; n < m_nt; n++){
            unsigned char r = bmp_buf[m*m_nt*3 + n*3 + 0];
            unsigned char g = bmp_buf[m*m_nt*3 + n*3 + 1];;
            unsigned char b = bmp_buf[m*m_nt*3 + n*3 + 2];;
            (buf+ m*m_nt*3 + n*3)[0] = b;
            (buf+ m*m_nt*3 + n*3)[1] = g;
            (buf+ m*m_nt*3 + n*3)[2] = r;
        }
    }

    if(m_qimg != NULL) {delete m_qimg->bits();delete m_qimg; m_qimg = NULL;}
    m_qimg = new QImage((uchar*)buf,m_nt,m_mt,m_nt * 3,
                        QImage::Format_RGB888);

    m_translated = false;

    return 0;
}


int XImage::translate(XImage &src)
{
    uchar *buf = new uchar[src.m_buf_size];
    if (buf == NULL) return -2;

    uchar *src_buf = src.m_qimg->bits();

    for(unsigned long m = 0; m < src.m_mt; m++) {
        for(unsigned long n = 0; n < src.m_nt; n++){
            unsigned char r = src_buf[m*src.m_nt*3 + n*3 + 0];
            unsigned char g = src_buf[m*src.m_nt*3 + n*3 + 1];
            unsigned char b = src_buf[m*src.m_nt*3 + n*3 + 2];
            (buf+ m*src.m_nt*3 + n*3)[0] = ((299 * r) + (587 * g) + (114 * b)) / 1000;//b;
            (buf+ m*src.m_nt*3 + n*3)[1] = ((299 * r) + (587 * g) + (114 * b)) / 1000;//g;
            (buf+ m*src.m_nt*3 + n*3)[2] = ((299 * r) + (587 * g) + (114 * b)) / 1000;//r;
        }
    }

    if(this->m_qimg != NULL) {delete m_qimg->bits();delete m_qimg; m_qimg = NULL;}
    this->m_qimg = new QImage((uchar*)buf,src.m_nt,src.m_mt,src.m_nt * 3,
                        QImage::Format_RGB888);

    this->m_buf_size = src.m_buf_size;
    this->m_m = src.m_m;
    this->m_n = src.m_n;
    this->m_mt = src.m_mt;
    this->m_nt = src.m_nt;
    this->m_x = src.m_x;
    this->m_y = src.m_y;

    m_translated = true;

    return 0;
}

int XImage::DrawWithTSTHPSV(XImage &src,PCTSTHPSV pTSTHPSV)
{
    uchar *buf = new uchar[src.m_buf_size];
    if (buf == NULL) return -2;

    uchar *src_buf = src.m_qimg->bits();

    for(unsigned long m = 0; m < src.m_mt; m++) {
        for(unsigned long n = 0; n < src.m_nt; n++){
            unsigned char r = src_buf[m*src.m_nt*3 + n*3 + 0];
            unsigned char g = src_buf[m*src.m_nt*3 + n*3 + 1];
            unsigned char b = src_buf[m*src.m_nt*3 + n*3 + 2];
            (buf+ m*src.m_nt*3 + n*3)[0] = ((299 * r) + (587 * g) + (114 * b)) / 1000;//b;
            (buf+ m*src.m_nt*3 + n*3)[1] = ((299 * r) + (587 * g) + (114 * b)) / 1000;//g;
            (buf+ m*src.m_nt*3 + n*3)[2] = ((299 * r) + (587 * g) + (114 * b)) / 1000;//r;
        }
    }
    PEPOINT pCurEP = NULL;
    for(pCurEP = pTSTHPSV->ptsth->pEPS;pCurEP != NULL;pCurEP = pCurEP->next){
        (buf+ pCurEP->point.n*3)[0] = 255;  //r;
        (buf+ pCurEP->point.n*3)[1] = 0;  //g;
        (buf+ pCurEP->point.n*3)[2] = 0;//b;
    }

    if(this->m_qimg != NULL) {
        delete m_qimg->bits();
        delete m_qimg; m_qimg = NULL;
    }
    this->m_qimg = new QImage((uchar*)buf,src.m_nt,src.m_mt,src.m_nt * 3,
                        QImage::Format_RGB888);

    this->m_buf_size = src.m_buf_size;
    this->m_m = src.m_m;
    this->m_n = src.m_n;
    this->m_mt = src.m_mt;
    this->m_nt = src.m_nt;
    this->m_x = src.m_x;
    this->m_y = src.m_y;

    m_translated = true;

    return 0;
}
